
package com.chuangan.releasetask.service.impl;

import com.chuangan.releasetask.Utils.FileUtils;
import com.chuangan.releasetask.Utils.KeyUtil;
import com.chuangan.releasetask.dto.StepDTO;
import com.chuangan.releasetask.dto.TaskDTO;
import com.chuangan.releasetask.enums.TaskStatusEnum;
import com.chuangan.releasetask.mapper.TaskMapper;
import com.chuangan.releasetask.pojo.TaskMaster;
import com.chuangan.releasetask.pojo.TaskStep;
import com.chuangan.releasetask.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: zihua
 * @Date: 2020/8/17 16:20
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class TaskServiceImpl  implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public TaskMaster create(TaskMaster taskMaster, MultipartRequest multipartRequest, HttpServletRequest req) throws Exception {
        String taskID = "task" + KeyUtil.genUniqueKey();
        Date startTime = new Date();
        Date endTime = new Date();
        taskMaster.setTaskID(taskID);
        taskMaster.setStartTime(startTime);
        endTime.setTime(taskMaster.getStartTime().getTime()+taskMaster.getCommitDeadline()*1000*60*60);
        taskMaster.setEndTime(endTime);
        taskMaster.setTaskStatus(TaskStatusEnum.NEW.getCode());
        taskMapper.create(taskMaster);
        for(TaskStep taskStep :  taskMaster.getTaskStepList()){
            taskStep.setStepID("step" + KeyUtil.genUniqueKey());
            taskStep.setTaskID(taskID);
            taskStep.setTaskImg(FileUtils.uploadImg(multipartRequest.getFile("image"+taskStep.getStepNum()),taskStep.getStepID(),"createTask"+"/"+taskMaster.getTaskPublisher()+"/"+taskMaster.getTaskID()+"/",req));
            taskMapper.save(taskStep);
        }
        return taskMaster;

    }

    @Override
    public boolean acceptTask(TaskDTO taskDTO,TaskMaster taskMaster) {
        //查询任务当前人数
        if(taskMaster.getFinishedNum()<taskMaster.getTaskNumber()){
            for(TaskStep taskStep  : taskDTO.getTaskStepList()){
                taskDTO.setProcessStatus(TaskStatusEnum.AUDITED.getCode());
                taskDTO.setEndTime(taskMaster.getEndTime());
                taskMapper.acceptTask(taskDTO,taskStep);
            }
            taskMapper.addProcessingTaskNum(taskDTO.getTaskID());
//            QuartzExecute.taskCommitQuartz("com.chuangan.releasetask.job.TaskJob");
            return true;
        }
        else
            return false;
    }

    @Override
    public boolean commitTask(TaskDTO taskDTO,MultipartRequest multipartRequest, HttpServletRequest req)  {
        //查询任务当前人数
        StepDTO stepDTO = new StepDTO();
        TaskMaster taskMaster = taskMapper.selectTaskById(taskDTO.getTaskID());
        if(taskMaster.getFinishedNum()<taskMaster.getTaskNumber()){
            for(TaskStep taskStep :  taskDTO.getTaskStepList()){
                    stepDTO.setTaskID(taskDTO.getTaskID());
                    stepDTO.setUserAccount(taskDTO.getUserAccount());
                    stepDTO.setStepNum(taskStep.getStepNum());
                    stepDTO.setTaskContext(taskStep.getTaskContext());
                    stepDTO.setTaskImg(FileUtils.uploadImg(multipartRequest.getFile("image"+taskStep.getStepNum()),stepDTO.getTaskID()+"_step"+taskStep.getStepNum(),"commitTask"+"/"+stepDTO.getUserAccount()+"/"+stepDTO.getTaskID()+"/",req));
                    stepDTO.setProcessStatus(TaskStatusEnum.FINISHED.getCode());
                    taskMapper.commitTask(stepDTO);
            }
            return true;
        }
        else
            return false;
    }









}

